//
//  EMTypeSegControl.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/1/4.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMTypeSegControl.h"

@implementation EMTypeSegControl
{
    UIView *segmentBackView;
    NSMutableArray *selectIndexArray;
    CGRect rect;
    UIImageView *gou;
    UIView *greenView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        selectIndexArray = [NSMutableArray array];
        _titleArray = [NSArray array];
        rect = frame;
    }
    
    return self;
}
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self initializeSegmentView];
}
- (void)initializeSegmentView
{
    
    segmentBackView = [[UIView alloc] initWithFrame:self.bounds];
    
    [self createSegmentItemWithNum:(int)_titleArray.count];
    
    
    
}

- (void)createSegmentItemWithNum: (int)num
{
    CGRect itemRect;
    
    itemRect.origin.y = 0;
    itemRect.size.width = rect.size.width / num;
    itemRect.size.height = rect.size.height;
    
    for (int i = 0; i < num; i ++) {
        itemRect.origin.x = i * (itemRect.size.width + 5);
        UIButton *segItem = [UIButton buttonWithType:UIButtonTypeCustom];
        segItem.frame = itemRect;
        segItem.layer.borderWidth = 1;
        segItem.layer.borderColor = RGB(200, 200, 200).CGColor;
        segItem.layer.cornerRadius = 5;
        [segItem setTitle:_titleArray[i] forState:UIControlStateNormal];
        [segItem setTitleColor:RGB(200, 200, 200) forState:UIControlStateNormal];
        segItem.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:12];
        [segItem addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventTouchUpInside];
        segItem.tag = 40200 + i;
        [selectIndexArray addObject:segItem];
        
        

        [self addSubview:segItem];
        
    }
    
    
    
}

- (void)segmentValueChanged: (UIButton *)sender
{
    self.selectedIndex = sender.tag - 40200;
    
    for (int i = 0; i < selectIndexArray.count; i ++) {
        UIView *select = selectIndexArray[i];

        if (sender.tag == select.tag) {
            
            [sender setTitleColor:RGB(112, 179, 81) forState:UIControlStateNormal];
            sender.layer.borderColor = RGB(112, 179, 81).CGColor;
            greenView = [[UIView alloc] initWithFrame:CGRectMake(sender.frame.size.width - 10, sender.frame.size.height - 10, 10, 10)];
            greenView.backgroundColor = RGB(112, 179, 81);
            gou = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_选中产品"]];
            gou.frame = CGRectMake(0, 0, 10, 10);
            [greenView addSubview:gou];
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:greenView.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = greenView.bounds;
            maskLayer.path = maskPath.CGPath;
            greenView.layer.mask = maskLayer;
            [sender addSubview:greenView];
            for (int j = 0; j < selectIndexArray.count; j ++) {
                if (j != i) {
                    UIButton *lastSelectButton = [selectIndexArray objectAtIndex:j];
                    lastSelectButton.layer.borderColor = RGB(200, 200, 200).CGColor;
                    [lastSelectButton setTitleColor:RGB(200, 200, 200) forState:UIControlStateNormal];
                    greenView = [[UIView alloc] initWithFrame:CGRectMake(sender.frame.size.width - 10, sender.frame.size.height - 10, 10, 10)];
                    greenView.backgroundColor = RGB(255, 255, 255);
                    gou = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
                    gou.frame = CGRectMake(0, 0, 10, 10);
                    [greenView addSubview:gou];
                    [lastSelectButton addSubview:greenView];
                }
                
            }
            
        }
        
    }
    
    if (self.actionBlock)
        self.actionBlock(self.selectedIndex);
    
}


@end
